
#include "Session.h"

ClientSession::ClientSession( boost::asio::io_service& ios ) : m_socket(ios)
{

}

tcp::socket& ClientSession::socket()
{
	return m_socket;
}

void ClientSession::start()
{
	m_socket.async_read_some(boost::asio::buffer(m_data, max_length),
		boost::bind(&ClientSession::handle_read, this,
		boost::asio::placeholders::error,
		boost::asio::placeholders::bytes_transferred));
}

void ClientSession::handle_read( const boost::system::error_code& error, size_t bytes_transferred )
{
	if (!error) {
		std::cout << m_socket.remote_endpoint().address() << std::endl;
		
		boost::asio::async_write(m_socket,
			boost::asio::buffer(m_data, bytes_transferred),
			boost::bind(&ClientSession::handle_write, this,
			boost::asio::placeholders::error));
		
		std::cout << m_data << std::endl;
	} else {
		delete this;
	}
}

void ClientSession::handle_write( const boost::system::error_code& error )
{
	if (!error) {
		std::cout << m_socket.remote_endpoint().address() << std::endl;
		
		m_socket.async_read_some(boost::asio::buffer(m_data, max_length),
			boost::bind(&ClientSession::handle_read, this,
			boost::asio::placeholders::error,
			boost::asio::placeholders::bytes_transferred));
	} else {
		delete this;
	}
}
